/*
 *  Quadtree.h
 *  
 *
 *  Created by Daniel Condon on 24/08/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#ifndef QUAD_GUARD
#define QUAD_GUARD

#include "../Creatures/Creature/Brain.h"
#include <vector>

class Quadtree {
private:
	double x, y;
	double width, height;
	//depth is how many nodes deep
	int depth;
	int maxDepth;
	vector<Brain*> objects;
	
	Quadtree* parent;
	Quadtree* NW;
	Quadtree* NE;
	Quadtree* SW;
	Quadtree* SE;
	
	bool Contains(Quadtree*, Brain*);
	bool InRange(Quadtree*, Vec, double);
public:
	/*Main Constructor
	 Params:
	 Parameter(0) : quadtree centre x coordinate
	 Parameter(1) : quadtree centre y coordinate
	 Parameter(2) : quadtree width(from centre)
	 Parameter(3) : quadtree height(from centre)
	 Parameter(4) : Current depth of this leaf node
	 Parameter(5) : maximum depth leaf nodes will traverse
	 */
	Quadtree(float, float, float, float, int, int);
	
	/*Deconstructor
	 Will recursively clear all nodes
	 */
	~Quadtree();
	
	/*--This function returns the nodes centre point as a Vec
	 Returns : node centre point as Vec class
	 */
	Vec getPos();
	
	/*--This function returns one of the 4 outer points creature the quad
	 */
	Vec getPoint(int);
	
	/*--This function returns the creature from the given point in the array
	 Params:
	 Parameter(0) : array slot of requested creature
	 Pre : requested slot not null and within array bounds
	 Returns : ptr to creature in given array slot
	 */
	Brain* getCreature(int);
	
	/*--This function adds the given creature controller to the array, and quadtree
	 Params:
	 Parameter(0) : ptr to creature to be added
	 Pre : ptr not void
	 */
	void AddCreature(Brain*);
	
	/*This function returns the creatures in the leaf containing the point
	 Params:
	 Parameter(0) : x coordinate
	 Parameter(1) : y coordinate
	 Pre : Parameter(0&1) are both within the quadtree area
	 Returns : a vector containing ptrs to all creatures in that specific leafnode
	 */
	vector<Brain*> GetCreaturesAt(double, double);
	
	/*This function returns the creatures with the given radius
	 Params:
	 Parameter(0) : x coordinate
	 Parameter(1) : y coordinate
	 Parameter(2) : radius of detection
	 Pre : Parameter(0&1) are both within the quadtree area
		   Parameter(2) not null
	 Returns : a vector containing ptrs to all creatures within specified radius
	 */
	vector<Brain*> GetCreaturesNear(double, double, double);
	
	/*--This function recursively removes all nodes of creatures
	 */
	void Clear();
	
	/*--This function returns the number of creatures in the current node
		(not including those in leaf nodes)
	 Returns : Total number of creatures held in the current node
	 */
	int getSize();
	
	/*--This function will return a vector containing the 4 corner points as Vec class
	 Returns : four(4) corner points as Vec class, in a vector container
	 */
	vector<Vec> points();
};

#endif